import pandas as pd
from io import StringIO

# 原始数据（包含14:54到14:55的交易）
data = """
时间,代码,名称,手数,均价,金额
14:55:58,920005,最佳箱线,23842,52.48000,125.12282
14:55:49,301548,票豪科技,52200,52.08000,271.85760
14:55:49,300564,筑弹设计,119600,17.94000,214.56240
14:55:45,002815,崇达技术,1005100,13.57000,1363.92070
14:55:29,503903,中纬股份,458700,8.45000,387.60150
14:55:27,002815,豪达技术,1006800,13.53000,1362.20040
14:55:13,301359,东南电子,57500,21.86000,125.69500
14:55:09,001221,C锂高,81800,53.08000,434.19440
14:55:05,500326,西藏天路,1652018,17.51000,2892.69352
14:55:03,002104,恒宝股份,1032826,18.64000,1925.18766
14:55:03,503507,振江股份,235900,25.86000,610.03740
14:54:26,588757,胜科纳米,38467,29.51000,113.51612
14:54:20,588757,胜科纳米,76617,29.46000,225.71368
14:54:12,001221,C锂高,37400,53.08000,198.51920
14:54:07,301529,福赛科技,50200,70.30000,352.90600
"""

# 创建完整DataFrame
full_df = pd.read_csv(StringIO(data))
full_df['时间'] = pd.to_datetime(full_df['时间'], format='%H:%M:%S')

# 分割成旧表和新表（旧表包含14:54的数据，新表包含14:55的数据）
old_df = full_df[full_df['时间'] < pd.to_datetime('14:55:00', format='%H:%M:%S')].copy()
new_df = full_df[full_df['时间'] >= pd.to_datetime('14:55:00', format='%H:%M:%S')].copy()

# 添加一些重复数据用于测试
duplicate_data = """
时间,代码,名称,手数,均价,金额
14:54:07,301529,福赛科技,50200,70.30000,352.90600
14:55:03,002104,恒宝股份,1032826,18.64000,1925.18766
"""
duplicate_df = pd.read_csv(StringIO(duplicate_data))
duplicate_df['时间'] = pd.to_datetime(duplicate_df['时间'], format='%H:%M:%S')

# 将重复数据添加到新表中
new_df = pd.concat([new_df, duplicate_df])

print("===== 旧表数据 (old_df) =====")
print(old_df)
print("\n===== 新表数据 (new_df) =====")
print(new_df)

# 找出新增数据（新表中有但旧表中没有的）
added_data = new_df[~new_df['时间'].astype(str).isin(old_df['时间'].astype(str))]

print("\n===== 新增数据 (added_data) =====")
print(added_data.sort_values('时间', ascending=False))

# 验证结果
print("\n验证结果:")
print(f"旧表记录数: {len(old_df)}")
print(f"新表记录数: {len(new_df)}")
print(f"新增记录数: {len(added_data)}")
print(f"重复记录数: {len(new_df) - len(duplicate_df) - len(added_data)}")